/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.aries.ui.widget.demo.slice;

 import com.aries.ui.util.FindViewUtil;
 import com.aries.ui.view.tab.IndicatorStyle;
 import com.aries.ui.view.tab.SlidingTabLayout;
 import com.aries.ui.view.tab.TextBold;
 import com.aries.ui.view.tab.listener.OnTabSelectListener;
 import com.aries.ui.view.tab.widget.MsgView;
 import com.aries.ui.view.title.TitleBarView;
 import com.aries.ui.widget.demo.ResourceTable;
 import com.aries.ui.widget.demo.module.tab.EventBusAbility;
 import com.aries.ui.widget.demo.module.tab.SimpleCardFragment;
 import com.aries.ui.widget.demo.module.tab.SwitchTabEvent;

 import ohos.aafwk.ability.AbilitySlice;
 import ohos.aafwk.content.Intent;
 import ohos.aafwk.content.Operation;

 import ohos.agp.components.*;
 import ohos.agp.utils.Color;
 import ohos.agp.utils.LayoutAlignment;
 import ohos.agp.window.service.WindowManager;

 import ohos.app.Context;

 import org.greenrobot.eventbus.EventBus;
 import org.greenrobot.eventbus.Subscribe;
 import org.greenrobot.eventbus.ThreadMode;

 import java.util.ArrayList;

 /**
  * SlidingTabAbilitySlice
  */
 public class SlidingTabAbilitySlice extends AbilitySlice implements OnTabSelectListener{
    private Context mContext = this;
    private ArrayList<Component> mFragments = new ArrayList<>();
    private final String[] mTitles = {
            "热门", "iOS", "phone"
            , "前端", "后端", "设计", "工具资源"
    };
    private MyPagerAdapter mAdapter;
    private SlidingTabLayout tabLayout_2;
    private PageSlider vp;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_sliding_tab);
        EventBus.getDefault().register(this);
        initTitleBar();
        initView();
    }

    private void initTitleBar() {
        TitleBarView titleBar = (TitleBarView) findComponentById(ResourceTable.Id_titleBar);
        titleBar.setTitleMainText("SlidingTabLayout示例").setOnLeftTextClickListener(component -> terminate());
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void initView() {
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        for (String title : mTitles) {
            SimpleCardFragment simpleCardFragment = new SimpleCardFragment(getContext());
            simpleCardFragment.setTitle(title);
            mFragments.add(simpleCardFragment);
        }

        Component decorView = findComponentById(ResourceTable.Id_scroll);
//        View decorView = getWindow().getDecorView();
        vp = FindViewUtil.getTargetView(decorView,ResourceTable.Id_vp);
        mAdapter = new MyPagerAdapter();
        vp.setProvider(mAdapter);

        /** 默认 */
        SlidingTabLayout tabLayout_1 = (SlidingTabLayout) findComponentById(ResourceTable.Id_tl_1);
        /**自定义部分属性*/
        tabLayout_2 = (SlidingTabLayout) findComponentById(ResourceTable.Id_tl_2);
        /** 字体加粗,大写 */
        SlidingTabLayout tabLayout_3 = (SlidingTabLayout) findComponentById(ResourceTable.Id_tl_3);
        /** tab固定宽度 */
        SlidingTabLayout tabLayout_4 = (SlidingTabLayout) findComponentById(ResourceTable.Id_tl_4);
        /** indicator固定宽度 */
        SlidingTabLayout tabLayout_5 = (SlidingTabLayout) findComponentById(ResourceTable.Id_tl_5);
        /** indicator圆 */
        SlidingTabLayout tabLayout_6 = (SlidingTabLayout) findComponentById(ResourceTable.Id_tl_6);
        /** indicator矩形圆角 */
        final SlidingTabLayout tabLayout_7 = (SlidingTabLayout) findComponentById(ResourceTable.Id_tl_7);
        /** indicator三角形 */
        SlidingTabLayout tabLayout_8 = (SlidingTabLayout) findComponentById(ResourceTable.Id_tl_8);
        /** indicator圆角色块 */
        SlidingTabLayout tabLayout_9 = (SlidingTabLayout) findComponentById(ResourceTable.Id_tl_9);
        /** indicator圆角色块 */
        SlidingTabLayout tabLayout_10 = (SlidingTabLayout) findComponentById(ResourceTable.Id_tl_10);

        tabLayout_1.setViewPager(vp);
        tabLayout_2.setViewPager(vp);
        tabLayout_2.setOnTabSelectListener(this);
        tabLayout_3.setViewPager(vp);
        tabLayout_4.setViewPager(vp);
        tabLayout_5.setViewPager(vp);
        tabLayout_6.setViewPager(vp);
        tabLayout_7.setViewPager(vp, mTitles);
        tabLayout_8. setViewPager(vp, mTitles, mFragments);
        tabLayout_9.setViewPager(vp);
        tabLayout_10.setViewPager(vp);

        vp.setCurrentPage(4);

        tabLayout_1.showDot(4);
        tabLayout_3.showDot(4);
        tabLayout_2.showDot(4);

        tabLayout_2.showMsg(3, 5);
        tabLayout_2.setMsgMargin(3, 0, 10);
        MsgView rtv_2_3 = tabLayout_2.getMsgView(3);
        if (rtv_2_3 != null) {
            rtv_2_3.setBackgroundColor(Color.getIntColor("#6D8FB0"));
        }

        tabLayout_2.showMsg(5, 5);
        tabLayout_2.setMsgMargin(5, 0, 10);
        SlidingTabLayout slidingTabLayout = new SlidingTabLayout(mContext);
        slidingTabLayout.getDelegate()
                .setIndicatorColor(Color.BLUE.getValue())
                .setIndicatorGravity(LayoutAlignment.BOTTOM)
                .setIndicatorHeight(3f)
                .setIndicatorStyle(IndicatorStyle.NORMAL)
                .setIndicatorWidthEqualTitle(false)
                .setTabPadding(6f)
                .setTextBold(TextBold.SELECT)
                .setTextSelectColor(Color.BLUE.getValue())
                .setTabSpaceEqual(false)
                .setTextUnSelectColor(Color.getIntColor("#99333333"))
                .setTextSelectSize(Text.TextSizeType.VP, 16f)
                .setTextSize(Text.TextSizeType.VP, 16f);

        ( (DirectionalLayout)tabLayout_1.getComponentParent()).addComponent(slidingTabLayout, new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, AttrHelper.vp2px(48,mContext)));
        slidingTabLayout.setViewPager(vp, mTitles);
        slidingTabLayout.setOnTabSelectListener(this);

        findComponentById(ResourceTable.Id_btn_eventTab).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                Intent intent = new Intent();
                Operation build = new Intent.OperationBuilder()
                        .withBundleName("com.aries.ui.widget.demo")
                        .withAbilityName(EventBusAbility.class)
                        .build();
                intent.setOperation(build);
                startAbility(intent);
            }
        });
    }

    @Override
    public void onTabSelect(int position) {
//        new ToastDialog(mContext).setText("onTabSelect&position--->" + position).show();
        vp.setCurrentPage(position);
    }

    @Override
    public void onTabReselect(int position) {
//        new ToastDialog(mContext).setText("onTabReselect&position--->" + position).show();
//        Toast.makeText(mContext, "onTabReselect&position--->" + position, Toast.LENGTH_SHORT).show();
    }

    private class MyPagerAdapter extends PageSliderProvider {


        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public Object createPageInContainer(ComponentContainer componentContainer, int i) {
            componentContainer.addComponent(mFragments.get(i));
            return componentContainer;
        }

        @Override
        public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
            componentContainer.removeComponent(mFragments.get(i));
        }

        @Override
        public boolean isPageMatchToObject(Component component, Object o) {
            return component==o;
        }

        @Override
        public String getPageTitle(int position) {
            return mTitles[position];
        }
    }

    /**
     * 主要演示 刷新上一页面tab时 Fragment 不同时切换问题{@link com.aries.ui.view.tab.utils.FragmentChangeManager#setFragments(int)}
     * 原{@link FragmentTransaction#commit()}修改为{@link FragmentTransaction#commitAllowingStateLoss()}.
     *
     * @param event SwitchTabEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(SwitchTabEvent event) {
        tabLayout_2.setCurrentTab(event.getPosition() == tabLayout_2.getCurrentTab() ? 1 : event.getPosition());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

}
